package menu

import (
	"context"
	"go-zero-project/services/user/rpc/pb"

	"go-zero-project/services/user/api/internal/svc"
	"go-zero-project/services/user/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type SaveMenuLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewSaveMenuLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SaveMenuLogic {
	return &SaveMenuLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *SaveMenuLogic) SaveMenu(req *types.SaveMenuReq) (resp *types.SaveMenuResp, err error) {
	// 1.builder rpc request
	menuReq := new(pb.SaveMenuReq)
	menuReq.MenuType = int32(req.MenuType)
	menuReq.Url = req.Url
	menuReq.Perms = req.Perms
	menuReq.Icon = req.Icon
	menuReq.Name = req.Name
	parentId := req.ParentId
	if parentId > 0 {
		menuReq.ParentId = parentId
	}
	menuReq.MenuType = int32(req.MenuType)
	menuReq.OrderNum = int32(req.OrderNum)
	// 2.request rpc
	menuResp, err := l.svcCtx.UserCenterClient.SaveMenu(l.ctx, menuReq)
	if err != nil {
		return nil, err
	}
	// 3.return obj
	return &types.SaveMenuResp{MenuId: menuResp.MenuId}, nil
}
